<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript">
function funSubmit(){
	Ajax.post("<%=request.getContextPath()%>/UserLeiServlet?method=save",funGetData(),function(responseText){
		//alert(responseText);
		funList();
		clear();
	});
}
function clear(){
// 	document.getElementById("ajaxForm").reset();
	document.getElementById("id").value="";
	document.getElementById("userName").value="";
	document.getElementById("password").value="";
}
function funList(){
	Ajax.get("<%=request.getContextPath()%>/UserLeiServlet?method=query",function(responseText){
		//JSON结构转换
		var myObject = eval('(' + responseText + ')');
		var tableId = document.getElementById("tableId");
		var currentRows = document.getElementById("tableId").rows.length; 
		for(var i=currentRows-1;i>0;i--){
			tableId.deleteRow(i);
			
		}
		for(var i in myObject){
			currentRows = document.getElementById("tableId").rows.length; 
			var insertTr = document.getElementById("tableId").insertRow(currentRows);
			var insertTd = insertTr.insertCell(0);
		    insertTd.style.textAlign="center";
			insertTd.innerHTML = myObject[i].userName;
			
			var insertTd2 = insertTr.insertCell(1);
		    insertTd2.style.textAlign="center";
			insertTd2.innerHTML = myObject[i].password;
			
			var insertTd3 = insertTr.insertCell(2);
			insertTd3.style.textAlign="center";
			insertTd3.innerHTML = format(myObject[i].creationTime);
			
			var insertTd4 = insertTr.insertCell(3);
			insertTd4.style.textAlign="center";
			insertTd4.innerHTML = "<a href='#'onclick='funDelete("+myObject[i].id+")'>删除</a>&nbsp;&nbsp;<a href='#' onclick='funEdit("+myObject[i].id+")'>修改<a>";
// 			删除<a>&nbsp;&nbsp;<a href='#' onclick='funEdit("+myObject[i].id+")'>修改<a>";
		}
	});
}
function funEdit(key) {
	Ajax.get("<%=request.getContextPath()%>/UserLeiServlet?method=edit&id="+key,function(responseText){
		
		var myObject = eval('(' + responseText + ')');
		document.getElementById("id").value=myObject.id;
		document.getElementById("userName").value=myObject.userName;
		document.getElementById("password").value=myObject.password;
	});
}
function funDelete(key) {
	Ajax.get("<%=request.getContextPath()%>/UserLeiServlet?method=delete&id="+ key, function(responseText) {

					if (responseText == "success") {
						funList();
					} else {
						alert("删除失败");
					}
				});
	}
	function funGetData() {
		var json = {
			"id" : document.getElementById("id").value,
			"userName" : document.getElementById("userName").value,
			"password" : document.getElementById("password").value,
			"creationTime" : document.getElementById("creationTime").value,
			"deleteLogo" : document.getElementById("deleteLogo").value
		};
		var jsonStr = JSON.stringify(json);
		return "data=" + jsonStr;
	}
	function add0(m) {
		return m < 10 ? '0' + m : m
	}
	function format(shijianchuo) {
		//shijianchuo是整数，否则要parseInt转换
		var time = new Date(shijianchuo);
		var y = time.getFullYear();
		var m = time.getMonth() + 1;
		var d = time.getDate();
		var h = time.getHours();
		var mm = time.getMinutes();
		var s = time.getSeconds();
		return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':'
				+ add0(mm) + ':' + add0(s);
	}
	function crtTimeFtt(value, row, index) {
		var crtTime = new Date(value);
		return top.dateFtt("yyyy-MM-dd hh:mm:ss", crtTime);//直接调用公共JS里面的时间类处理的办法     
	}
</script>
</head>
<body>

	<table id="tableId" border="1">
		<tr>

			<td>用户名称</td>
			<td>密码</td>
			<td>创建时间</td>
			<td>操作</td>

		</tr>
	</table>
<form id="ajaxForm">
	    	<input type="hidden" id="id" /> 
	用户名称:<input type="text" id="userName" /> 
	密码:<input type="text" id="password" />
	<input type="hidden" id="creationTime" />
	<input type="hidden" id="deleteLogo" />
</form>
	<input type="button" id="submit1" value="保存" onclick="funSubmit()" />
	<!--  <input type="button" id="submit2" value="修改"onclick="funEdit()" />-->
	<!-- <input type="button" id="submit3" value="删除" onclick="funDelete()" /> -->
	<input type="button" id="submit4" value="查询" onclick="funList()" />
</head>
<body>

</body>
</html>